#include "Common.h"

initSing(LfgSystem);

LfgSystem::LfgSystem()
{
	for(ui32 i = 0; i < NUM_LFG_TYPES; ++i)
	{
		switch(i)
		{
		case 0: // actually we wont use this list at all, cause its LFG_NONE
			{
				m_queues[i] = new list<PlayerLfgInfo*>;
				break;
			}
		case 1:
			{
				m_queues[i] = new list<PlayerLfgInfo*>[500]; // TODO: Find how many possible dungeons there are!
				break;
			}
		case 2:
			{
				m_queues[i] = new list<PlayerLfgInfo*>[500]; // TODO: Find how many raids there are!
				break;
			}
		case 3:
			{
				m_queues[i] = new list<PlayerLfgInfo*>; // maybe well find a better way for this...
				break;
			}
		case 4:
			{
				m_queues[i] = new list<PlayerLfgInfo*>[500]; // id say about 244 zones
				break;
			}
		case 5:
			{
				m_queues[i] = new list<PlayerLfgInfo*>[500]; // TODO: same as 1
				break;
			}
		}
	}
	DBCStore<DungeonType> tmpstore;
	tmpstore.Load("dbc\\LFGDungeons.dbc", "uuuuuuuuuuuuuuuuuuuuuuuu");
	for(ui32 i = 0; i < tmpstore.GetRowCount(); ++i)
	{
		DungeonType* dt = tmpstore.GetRow(i);
		if(dt->id[0] < 500)
			DungeonTypes[dt->id[0]] = dt->id[20];
	}
}

void LfgSystem::QueuePlayer(Player* pPlayer, LfgType queuetype, ui32 id, ui32 queueid)
{
	switch(queuetype)
	{
	case LFG_TYPE_NONE: // remove player from queue
		{
			LfgInfo inf = pPlayer->GetLfgInfo(queueid);
			if(inf.type && inf.type < NUM_LFG_TYPES)
			{
				switch(inf.type)
				{
				case LFG_TYPE_NONE:
					return;

				case LFG_TYPE_RAID:
				case LFG_TYPE_HEROIC:
				case LFG_TYPE_DUNGEON:
					if(inf.id >= 500)
						return;
					break;
				case LFG_TYPE_ZONE:
					if(inf.id >= 500)
						return;
					break;
				}
				if(inf.type != LFG_TYPE_QUEST)
				{
					list<PlayerLfgInfo*>::iterator itr = m_queues[inf.type][inf.id].begin();
					list<PlayerLfgInfo*>::iterator end = m_queues[inf.type][inf.id].end();
					
					for( ; itr != end; ++itr)
					{
						if((*itr)->pPlayer == pPlayer && (*itr)->queueid == queueid)
						{
							PlayerLfgInfo* info = (*itr);
							delete info;
							m_queues[inf.type][inf.id].erase(itr);
							Log->Notice("Lfg", "Player erased!");
							return;
						}
					}
				}
				else
				{
					list<PlayerLfgInfo*>::iterator itr = m_queues[inf.type]->begin();
					list<PlayerLfgInfo*>::iterator end = m_queues[inf.type]->end();
					for( ; itr != end; ++itr)
					{
						if((*itr)->pPlayer == pPlayer && (*itr)->questid == id && (*itr)->queueid == queueid)
						{
							PlayerLfgInfo* info = (*itr);
							delete info;
							m_queues[inf.type]->erase(itr);
							Log->Notice("Lfg", "Player erased!");
							return;
						}
					}
				}
			}
			break;
		}
	case LFG_TYPE_RAID:
	case LFG_TYPE_HEROIC:
	case LFG_TYPE_DUNGEON:
		{
			if(id < 500)
			{
				m_queues[queuetype][id].push_back(new PlayerLfgInfo(pPlayer, queueid));
				pPlayer->SetLfgInfo(queueid, queuetype, id);
			}
			break;
		}
	case LFG_TYPE_ZONE:
		{
			if(id < 500)
			{
				m_queues[queuetype][id].push_back(new PlayerLfgInfo(pPlayer, queueid));
				pPlayer->SetLfgInfo(queueid, queuetype, id);
			}
			break;
		}
	case LFG_TYPE_QUEST:
		{
			m_queues[queuetype]->push_back(new PlayerLfgInfo(pPlayer, queueid, id));
			pPlayer->SetLfgInfo(queueid, queuetype, id);
			break;
		}
	}
}